<!doctype html>
<html lang="zh-CN">
<head>
	<meta charset="utf-8">
	<title>Laravel - 为 WEB 艺术家创造的 PHP 框架。 | Laravel 中文网</title>
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="author" content="Laravel 中文网">
	<meta name="description" content="Laravel - 为 WEB 艺术家创造的 PHP 框架。| Laravel 中文网">
	<meta name="keywords" content="Laravel中文社区,php框架,laravel中文网,php framework,restful routing,laravel,laravel php">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!--[if lte IE 9]>
		<script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
	<![endif]-->
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="../../assets/css/laravel.css">
</head>
<body class="docs language-php">

	<span class="overlay"></span>

	<nav class="main">
		<div class="container">
			<a href="../../index.html" class="brand">
				<img src="../../assets/img/laravel-logo.png" height="30">
				Laravel
			</a>

			<div class="responsive-sidebar-nav">
				<a href="#" class="toggle-slide menu-link btn">&#9776;</a>
			</div>

				<div class="switcher">
					<div class="dropdown">
						<button class="btn dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
							<!--<span class="faint">v</span> -->
							4.0
							<span class="caret"></span>
						</button>
						<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="../5.0/index.html">5.0</a>
								</li>
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="../4.2/index.html">4.2</a>
								</li>
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="../4.1/index.html">4.1</a>
								</li>
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="index.html">4.0</a>
								</li>
						</ul>
					</div>
				</div>

			<ul class="main-nav">
				<li class="nav-docs"><a href="../index.html">中文文档</a></li>
				<li class="nav-community"><a href="http://wenda.golaravel.com" target="_blank">问答社区</a></li>
				<li class="nav-api"><a href="http://laravel.com/api/5.0/" target="_blank">API</a></li>
				<li class="nav-laracasts"><a href="https://laracasts.com" target="_blank">视频教程（国外）</a></li>
				<li class="nav-forge"><a href="https://forge.laravel.com" target="_blank">Forge</a></li>
				
			</ul>
		</div>
	</nav>

		<nav id="slide-menu" class="slide-menu" role="navigation">
		
		<div class="brand">
			<a href="../../index.html">
				<img src="../../assets/img/laravel-logo-white.png" height="50">
			</a>
		</div>

		<ul class="slide-main-nav">
			<li><a href="../../index.html">首页</a></li>
			<li class="nav-docs"><a href="../index.html">中文文档</a></li>
			<li class="nav-community"><a href="http://wenda.golaravel.com" target="_blank">问答社区</a></li>
			<li class="nav-api"><a href="http://laravel.com/api/5.0/" target="_blank">API</a></li>
			<li class="nav-laracasts"><a href="https://laracasts.com" target="_blank">视频教程（国外）</a></li>
			<li class="nav-forge"><a href="https://forge.laravel.com" target="_blank">Forge</a></li>
			
		</ul>

		<div class="slide-docs-nav">
			<h2>文档目录</h2>
			<ul>
<li>序言
<ul>
<li><a href="introduction.html">简介</a></li>
<li><a href="quick.html">快速入门</a></li>
<li><a href="contributing.html">为Laravel做贡献</a></li>
</ul>
</li>
<li>开始学习
<ul>
<li><a href="installation.html">安装</a></li>
<li><a href="configuration.html">配置</a></li>
<li><a href="lifecycle.html">Request的生命周期</a></li>
<li><a href="routing.html">路由</a></li>
<li><a href="requests.html">请求与输入</a></li>
<li><a href="responses.html">视图 &amp; Response</a></li>
<li><a href="controllers.html">控制器</a></li>
<li><a href="errors.html">错误 &amp; 日志</a></li>
</ul>
</li>
<li>深度历险
<ul>
<li><a href="cache.html">缓存</a></li>
<li><a href="extending.html">核心扩展</a></li>
<li><a href="events.html">事件</a></li>
<li><a href="facades.html">Facades</a></li>
<li><a href="html.html">表单 &amp; HTML</a></li>
<li><a href="helpers.html">有用的工具函数</a></li>
<li><a href="ioc.html">IoC 容器</a></li>
<li><a href="localization.html">本地化</a></li>
<li><a href="mail.html">邮件</a></li>
<li><a href="packages.html">扩展包开发</a></li>
<li><a href="pagination.html">分页</a></li>
<li><a href="queues.html">队列</a></li>
<li><a href="security.html">安全</a></li>
<li><a href="session.html">Session</a></li>
<li><a href="templates.html">模版</a></li>
<li><a href="testing.html">单元测试</a></li>
<li><a href="validation.html">验证</a></li>
</ul>
</li>
<li>数据库
<ul>
<li><a href="database.html">基本用法</a></li>
<li><a href="queries.html">查询构造器</a></li>
<li><a href="eloquent.html">Eloquent ORM</a></li>
<li><a href="schema.html">结构生成器</a></li>
<li><a href="migrations.html">迁移 &amp; 数据填充</a></li>
<li><a href="redis.html">Redis</a></li>
</ul>
</li>
<li>Artisan 命令行工具
<ul>
<li><a href="artisan.html">概览</a></li>
<li><a href="commands.html">Artisan 开发</a></li>
</ul>
</li>
<li>贡献者
<ul>
<li><a href="contributors.html">中文翻译贡献者名单</a></li>
</ul>
</li>
</ul>

		</div>

	</nav>

	<div class="docs-wrapper container">

		<section class="sidebar">
			<ul>
<li>序言
<ul>
<li><a href="introduction.html">简介</a></li>
<li><a href="quick.html">快速入门</a></li>
<li><a href="contributing.html">为Laravel做贡献</a></li>
</ul>
</li>
<li>开始学习
<ul>
<li><a href="installation.html">安装</a></li>
<li><a href="configuration.html">配置</a></li>
<li><a href="lifecycle.html">Request的生命周期</a></li>
<li><a href="routing.html">路由</a></li>
<li><a href="requests.html">请求与输入</a></li>
<li><a href="responses.html">视图 &amp; Response</a></li>
<li><a href="controllers.html">控制器</a></li>
<li><a href="errors.html">错误 &amp; 日志</a></li>
</ul>
</li>
<li>深度历险
<ul>
<li><a href="cache.html">缓存</a></li>
<li><a href="extending.html">核心扩展</a></li>
<li><a href="events.html">事件</a></li>
<li><a href="facades.html">Facades</a></li>
<li><a href="html.html">表单 &amp; HTML</a></li>
<li><a href="helpers.html">有用的工具函数</a></li>
<li><a href="ioc.html">IoC 容器</a></li>
<li><a href="localization.html">本地化</a></li>
<li><a href="mail.html">邮件</a></li>
<li><a href="packages.html">扩展包开发</a></li>
<li><a href="pagination.html">分页</a></li>
<li><a href="queues.html">队列</a></li>
<li><a href="security.html">安全</a></li>
<li><a href="session.html">Session</a></li>
<li><a href="templates.html">模版</a></li>
<li><a href="testing.html">单元测试</a></li>
<li><a href="validation.html">验证</a></li>
</ul>
</li>
<li>数据库
<ul>
<li><a href="database.html">基本用法</a></li>
<li><a href="queries.html">查询构造器</a></li>
<li><a href="eloquent.html">Eloquent ORM</a></li>
<li><a href="schema.html">结构生成器</a></li>
<li><a href="migrations.html">迁移 &amp; 数据填充</a></li>
<li><a href="redis.html">Redis</a></li>
</ul>
</li>
<li>Artisan 命令行工具
<ul>
<li><a href="artisan.html">概览</a></li>
<li><a href="commands.html">Artisan 开发</a></li>
</ul>
</li>
<li>贡献者
<ul>
<li><a href="contributors.html">中文翻译贡献者名单</a></li>
</ul>
</li>
</ul>

		</section>

		<article>
			<h1>事件</h1>
<ul>
<li><a href="#basic-usage">基本用法</a></li>
<li><a href="#wildcard-listeners">利用通配符匹配多个事件</a></li>
<li><a href="#using-classes-as-listeners">使用类作为监听器</a></li>
<li><a href="#queued-events">事件队列</a></li>
<li><a href="#event-subscribers">事件订阅者</a></li>
</ul>
<p><a name="basic-usage"></a></p>
<h2>基本用法</h2>
<p>Laravel中的<code>Event</code>类实现了简单的观察者模式，允许你在应用程序中订阅和监听事件。</p>
<p><strong>订阅事件</strong></p>
<pre><code>Event::listen('user.login', function($user)
{
    $user-&gt;last_login = new DateTime;

    $user-&gt;save();
});
</code></pre>
<p><strong>触发事件</strong></p>
<pre><code>$event = Event::fire('user.login', array($user));
</code></pre>
<p>你也可以在订阅事件时指定优先级。优先级高的监听器将会被优先执行，相同优先级的将被按照订阅顺序执行。</p>
<p><strong>带有优先级的事件订阅</strong></p>
<pre><code>Event::listen('user.login', 'LoginHandler', 10);

Event::listen('user.login', 'OtherHandler', 5);
</code></pre>
<p>如果希望阻止事件传播到其他监听器，只需让监听器返回<code>false</code>即可实现：</p>
<p><strong>阻止事件传播</strong></p>
<pre><code>Event::listen('user.login', function($event)
{
    // Handle the event...

    return false;
});
</code></pre>
<p><a name="wildcard-listeners"></a></p>
<h2>利用通配符匹配多个事件</h2>
<p>注册事件监听器时，可以使用星号匹配多个事件进行监听：</p>
<p><strong>注册多个事件的监听器</strong></p>
<pre><code>Event::listen('foo.*', function($param, $event)
{
    // Handle the event...
});
</code></pre>
<p>该监听器将会处理所有以<code>foo.</code>开始的事件。注意：完整的事件名将以事件处理器的最后一个参数传入。</p>
<p><a name="using-classes-as-listeners"></a></p>
<h2>使用类作为监听器</h2>
<p>在某些情况下，你可能希望使用类来代替闭包处理事件。类事件监听器（Class event listener）通过 <a href="../ioc">Laravel IoC 容器</a>实现，为监听器提供了强大的依赖注入的能力。</p>
<p><strong>注册一个类监事件听器</strong></p>
<pre><code>Event::listen('user.login', 'LoginHandler');
</code></pre>
<p>默认情况下，<code>LoginHandler</code>类的<code>handle</code>方法将被调用：</p>
<p><strong>定义一个事件监听器类</strong></p>
<pre><code>class LoginHandler {

    public function handle($data)
    {
        //
    }

}
</code></pre>
<p>如果你不想使用默认的<code>handle</code>方法，你可以在订阅时指定方法：</p>
<p><strong>指定实现订阅的方法</strong></p>
<pre><code>Event::listen('user.login', 'LoginHandler@onLogin');
</code></pre>
<p><a name="queued-events"></a></p>
<h2>事件队列</h2>
<p>使用<code>queue</code>和<code>flush</code>方法，可以将事件放入&quot;队列&quot;中，而不是立刻执行：</p>
<p><strong>向事件队列中添加一个事件</strong></p>
<pre><code>Event::queue('foo', array($user));
</code></pre>
<p><strong>注册一个事件清除器</strong></p>
<pre><code>Event::flusher('foo', function($user)
{
    //
});
</code></pre>
<p>最后，你可以使用<code>flush</code>方法清除掉队列中的所有事件：</p>
<pre><code>Event::flush('foo');
</code></pre>
<p><a name="event-subscribers"></a></p>
<h2>事件订阅者</h2>
<p>事件订阅者类可以在其自身内部订阅多个事件。并且事件订阅者应该定义<code>subscribe</code>方法，并接收一个事件调度器实例作为参数：</p>
<p><strong>定义一个事件订阅者</strong></p>
<pre><code>class UserEventHandler {

    /**
     * Handle user login events.
     */
    public function onUserLogin($event)
    {
        //
    }

    /**
     * Handle user logout events.
     */
    public function onUserLogout($event)
    {
        //
    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param  Illuminate\Events\Dispatcher  $events
     * @return array
     */
    public function subscribe($events)
    {
        $events-&gt;listen('user.login', 'UserEventHandler@onUserLogin');

        $events-&gt;listen('user.logout', 'UserEventHandler@onUserLogout');
    }

}
</code></pre>
<p>一旦定义了事件订阅者，就可以使用<code>Event</code>类进行注册。</p>
<p><strong>注册一个事件订阅者</strong></p>
<pre><code>$subscriber = new UserEventHandler;

Event::subscribe($subscriber);
</code></pre>
<p>译者：王赛  <a href="http://www.bootcss.com" target="_blank">（Bootstrap中文网）</a></p>

		</article>
	</div>


	<footer class="main">
		<ul>
			<li class="nav-docs"><a href="../index.html">中文文档</a></li>
			<li class="nav-community"><a href="http://wenda.golaravel.com" target="_blank">问答社区</a></li>
			<li class="nav-api"><a href="http://laravel.com/api/5.0/" target="_blank">API</a></li>
			<li class="nav-laracasts"><a href="https://laracasts.com" target="_blank">视频教程（国外）</a></li>
			<li class="nav-forge"><a href="https://forge.laravel.com" target="_blank">Forge</a></li>
			
		</ul>
		<p>Laravel is a trademark of Taylor Otwell. Copyright &copy; Taylor Otwell.</p>
		<p class="less-significant"><a href="http://jackmcdade.com" target="_blank">Design by Jack McDade</a></p>
	</footer>

	<script src="../../assets/js/laravel.js"></script>
	<script src="../../assets/js/viewport-units-buggyfill.js"></script>
	<script>window.viewportUnitsBuggyfill.init();</script>
	<script type="text/javascript">
	var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
	document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Fc8d13872a523d9c286aa7affbe0921f1' type='text/javascript'%3E%3C/script%3E"));
	</script>
</body>
</html>
